package com.swsc.solarverse.service.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.swsc.solarverse.common.config.ResParams;
import com.swsc.solarverse.common.enums.DeviceSourceEnums;
import com.swsc.solarverse.common.enums.DeviceTypeEnums;
import com.swsc.solarverse.common.enums.StateEnums;
import com.swsc.solarverse.common.util.LocalDateTimeUtils;
import com.swsc.solarverse.common.util.ObjectMapper;
import com.swsc.solarverse.common.util.RegexUtils;
import com.swsc.solarverse.dao.entity.analysis.StringEfficiencyDayDO;
import com.swsc.solarverse.dao.entity.analysis.StringEfficiencyRecordDO;
import com.swsc.solarverse.dao.entity.forward.*;
import com.swsc.solarverse.dao.entity.sys.CorporationDO;
import com.swsc.solarverse.dao.mapper.CustomSqlMapper;
import com.swsc.solarverse.service.analysis.IStringEfficiencyDayService;
import com.swsc.solarverse.service.analysis.IStringEfficiencyRecordService;
import com.swsc.solarverse.service.common.ForwardDeviceDataHelper;
import com.swsc.solarverse.service.forward.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @Author chencl
 * @Date 2024/7/22 9:41
 * @Version 1.0
 * @Description
 */
@lombok.extern.slf4j.Slf4j
@org.springframework.stereotype.Service
public class StringIuAnalysisService {
    private final IForwardDeviceInfoService forwardDeviceInfoService;
    private final IForwardStringDataCurrentdayService forwardStringDataCurrentdayService;
    private final CustomSqlMapper customSqlMapper;
    private final IForwardStringLeDiagnoseRecordService forwardStringLeDiagnoseRecordService;
    private final IStringEfficiencyRecordService stringEfficiencyRecordService;
    private final IForwardSubmatrixLeRecordService forwardSubmatrixLeRecordService;
    private final IStringEfficiencyDayService stringEfficiencyDayService;
    private final IForwardStringLeDiagnoseDetailService forwardStringLeDiagnoseDetailService;

    public StringIuAnalysisService(IForwardDeviceInfoService forwardDeviceInfoService,
                                   IForwardStringDataCurrentdayService forwardStringDataCurrentdayService,
                                   CustomSqlMapper customSqlMapper,
                                   IForwardStringLeDiagnoseRecordService forwardStringLeDiagnoseRecordService,
                                   IStringEfficiencyRecordService stringEfficiencyRecordService,
                                   IForwardSubmatrixLeRecordService forwardSubmatrixLeRecordService,
                                   IStringEfficiencyDayService stringEfficiencyDayService,
                                   IForwardStringLeDiagnoseDetailService forwardStringLeDiagnoseDetailService) {
        this.forwardDeviceInfoService = forwardDeviceInfoService;
        this.forwardStringDataCurrentdayService = forwardStringDataCurrentdayService;
        this.customSqlMapper = customSqlMapper;
        this.forwardStringLeDiagnoseRecordService = forwardStringLeDiagnoseRecordService;
        this.stringEfficiencyRecordService = stringEfficiencyRecordService;
        this.forwardSubmatrixLeRecordService = forwardSubmatrixLeRecordService;
        this.stringEfficiencyDayService = stringEfficiencyDayService;
        this.forwardStringLeDiagnoseDetailService = forwardStringLeDiagnoseDetailService;
    }

    public void analysisIU(CorporationDO corporationDo, LocalDateTime now) {
        if (null == corporationDo) {
            return;
        }
        Integer tenantNo = corporationDo.getTenantNo();
        Integer organNo = corporationDo.getOrganNo();
        LocalDateTime maxCurrentDateTime = forwardStringDataCurrentdayService.maxDataTimeByOrgan(organNo);
        if (null == maxCurrentDateTime) {
            log.info("组串IU分析:实时数据暂无数据");
            return;
        }
        List<ForwardDeviceInfoDO> forwardDeviceInfoDos = forwardDeviceInfoService.list(Wrappers.lambdaQuery(ForwardDeviceInfoDO.class)
                .eq(ForwardDeviceInfoDO::getTenantNo, tenantNo)
                .eq(ForwardDeviceInfoDO::getOrganNo, organNo)
                .eq(ForwardDeviceInfoDO::getDeviceSource, DeviceSourceEnums.SCADA.getCode())
                .in(ForwardDeviceInfoDO::getDeviceType, Arrays.asList(DeviceTypeEnums.INVERTER.getType(), DeviceTypeEnums.COMBINER_BOX.getType(), DeviceTypeEnums.STRR.getType()))
        );
        log.info("低效组串分析计算-查询所有设备信息: {}, {}", organNo, forwardDeviceInfoDos.size());
        if (CollectionUtils.isEmpty(forwardDeviceInfoDos)) {
            log.info("低效组串分析计算-未查询到设备信息: {}, {}", organNo, forwardDeviceInfoDos.size());
            return;
        }
        List<ForwardDeviceInfoDO> inverterForwardDeviceInfoDos = forwardDeviceInfoDos.stream().filter(fdid -> DeviceTypeEnums.INVERTER.getType().equals(fdid.getDeviceType())).collect(Collectors.toList());
        log.info("低效组串分析计算-查询到逆变器设备数量: {}, {}", organNo, inverterForwardDeviceInfoDos.size());
        List<ForwardDeviceInfoDO> combinerboxForwardDeviceInfoDos = forwardDeviceInfoDos.stream().filter(fdid -> DeviceTypeEnums.COMBINER_BOX.getType().equals(fdid.getDeviceType())).collect(Collectors.toList());
        log.info("低效组串分析计算-查询到汇流箱设备数量: {}, {}", organNo, combinerboxForwardDeviceInfoDos.size());
        List<ForwardStringLeDiagnoseRecordDO> leDiagnoseRecordResults = new ArrayList<>(forwardDeviceInfoDos.size() * 18);
        if (CollectionUtils.isNotEmpty(inverterForwardDeviceInfoDos)) {
            // 按照子阵分组
            Map<String, List<ForwardDeviceInfoDO>> submatrixInverterForwardDeviceMap = inverterForwardDeviceInfoDos.stream().collect(Collectors.groupingBy(fdid -> RegexUtils.extractSubmatrixCode(fdid.getDeviceCode())));
            List<ForwardStringLeDiagnoseRecordDO> inverterLeDiagnoseRecords = processSubmatrixForwardDeviceMap(corporationDo, submatrixInverterForwardDeviceMap, DeviceTypeEnums.INVERTER, now, now);
            if (CollectionUtils.isNotEmpty(inverterLeDiagnoseRecords)) {
                leDiagnoseRecordResults.addAll(inverterLeDiagnoseRecords);
            }
        }
        if (CollectionUtils.isNotEmpty(combinerboxForwardDeviceInfoDos)) {
            // 按照子阵分组
            Map<String, List<ForwardDeviceInfoDO>> submatrixCombinerboxForwardDeviceMap = combinerboxForwardDeviceInfoDos.stream().collect(Collectors.groupingBy(fdid -> RegexUtils.extractSubmatrixCode(fdid.getDeviceCode())));
            List<ForwardStringLeDiagnoseRecordDO> combinerboxLeDiagnoseRecords = processSubmatrixForwardDeviceMap(corporationDo, submatrixCombinerboxForwardDeviceMap, DeviceTypeEnums.COMBINER_BOX, now, now);
            if (CollectionUtils.isNotEmpty(combinerboxLeDiagnoseRecords)) {
                leDiagnoseRecordResults.addAll(combinerboxLeDiagnoseRecords);
            }
        }

        if (CollectionUtils.isNotEmpty(leDiagnoseRecordResults)) {
            leDiagnoseRecordResults.sort(new Comparator<ForwardStringLeDiagnoseRecordDO>() {
                @Override
                public int compare(ForwardStringLeDiagnoseRecordDO o1, ForwardStringLeDiagnoseRecordDO o2) {
                    return o1.getDeviceCode().compareTo(o2.getDeviceCode());
                }
            });
        }
        // 同一子阵逆变器-组串和汇流箱-字串不会混搭
        log.info("低效组串分析计算-保存结果数据开始: {}", leDiagnoseRecordResults.size());
        int cnt = forwardStringLeDiagnoseRecordService.insertIgnoreBatch(leDiagnoseRecordResults);
        log.info("低效组串分析计算-保存结果数据结束: {}", cnt);

    }

    public List<ForwardStringLeDiagnoseRecordDO> processSubmatrixForwardDeviceMap(CorporationDO corporationDo, Map<String, List<ForwardDeviceInfoDO>> submatrixForwardDeviceMap,
                                                                                  DeviceTypeEnums deviceTypeEnum, LocalDateTime dataTime, LocalDateTime dateTime) {
        if (null != corporationDo && null != submatrixForwardDeviceMap && submatrixForwardDeviceMap.size() > 0) {
            if (null == deviceTypeEnum) {
                deviceTypeEnum = DeviceTypeEnums.INVERTER;
            }
            LocalDateTime dateTimeGe = dataTime.withHour(11).withMinute(1).withSecond(0).withNano(0);
            LocalDateTime dateTimeLt = dataTime.withHour(12).withMinute(0).withSecond(0).withNano(0);
            List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordResults = new ArrayList<>(submatrixForwardDeviceMap.size() * 18);
            if (DeviceTypeEnums.INVERTER == deviceTypeEnum) {
                for (Map.Entry<String, List<ForwardDeviceInfoDO>> submatrixForwardDeviceMapEntry : submatrixForwardDeviceMap.entrySet()) {
                    if (ResParams.collectingDistributingSubmatrixCodes().contains(submatrixForwardDeviceMapEntry.getKey())) {
                        log.info("低效组串分析计算-该子阵为集散式逆变器: {}", submatrixForwardDeviceMapEntry.getKey());
                        continue;
                    }
                    List<String> deviceUidIn = submatrixForwardDeviceMapEntry.getValue().stream().map(ForwardDeviceInfoDO::getDeviceUid).collect(Collectors.toList());
                    List<Map<String, Object>> submatrixInverterDataList = null; // customSqlMapper.queryInverterDataByTmstampRange(deviceUidIn, dateTimeGe, dateTimeLt);
                    log.info("低效组串分析计算-查询逆变器设备电压电流数据: {}, {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), submatrixInverterDataList.size());
                    if (CollectionUtils.isEmpty(submatrixInverterDataList)) {
                        log.info("低效组串分析计算-未查询到逆变器设备电压电流数据: {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey());
                        continue;
                    }
                    List<ForwardStringDataDO> submatrixForwardStringDataDos = ForwardDeviceDataHelper.processInverterData(submatrixForwardDeviceMapEntry.getValue(), submatrixInverterDataList, dateTime);
                    log.info("低效组串分析计算-转换逆变器设备电压电流数据: {}, {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), submatrixForwardStringDataDos.size());
                    if (CollectionUtils.isEmpty(submatrixForwardStringDataDos)) {
                        log.info("低效组串分析计算-转换逆变器设备电压电流数据后无数据: {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey());
                        continue;
                    }
                    // 计算组串能效判断
                    ForwardDeviceDataHelper.calculateStringIURecordBySubmatrix(corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), dataTime, dateTime,
                            submatrixForwardStringDataDos, stringLeDiagnoseRecordResults);
                }
            } else if (DeviceTypeEnums.COMBINER_BOX == deviceTypeEnum) {
                for (Map.Entry<String, List<ForwardDeviceInfoDO>> submatrixForwardDeviceMapEntry : submatrixForwardDeviceMap.entrySet()) {
                    List<String> deviceUidIn = submatrixForwardDeviceMapEntry.getValue().stream().map(ForwardDeviceInfoDO::getDeviceUid).collect(Collectors.toList());
                    List<Map<String, Object>> submatrixCombinerboxDataList = null; // customSqlMapper.queryCombinerboxDataByTmstampRange(deviceUidIn, dateTimeGe, dateTimeLt);
                    log.info("低效组串分析计算-查询汇流箱设备电压电流数据: {}, {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), submatrixCombinerboxDataList.size());
                    if (CollectionUtils.isEmpty(submatrixCombinerboxDataList)) {
                        log.info("低效组串分析计算-未查询到汇流箱设备电压电流数据: {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey());
                        continue;
                    }
                    List<ForwardStringDataDO> submatrixForwardStringDataDos = ForwardDeviceDataHelper.processCombinerboxData(submatrixForwardDeviceMapEntry.getValue(), submatrixCombinerboxDataList, dateTime);
                    log.info("低效组串分析计算-转换汇流箱设备电压电流数据: {}, {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), submatrixForwardStringDataDos.size());
                    if (CollectionUtils.isEmpty(submatrixForwardStringDataDos)) {
                        log.info("低效组串分析计算-转换汇流箱设备电压电流数据后无数据: {}, {}", corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey());
                        continue;
                    }
                    // 计算组串能效判断
                    ForwardDeviceDataHelper.calculateStringIURecordBySubmatrix(corporationDo.getOrganNo(), submatrixForwardDeviceMapEntry.getKey(), dataTime, dateTime,
                            submatrixForwardStringDataDos, stringLeDiagnoseRecordResults);
                }
            }
            return stringLeDiagnoseRecordResults;
        }

        return null;
    }


    public void analysisNew(Integer tenantNo, Integer organNo, LocalDateTime dataTime, LocalDateTime dateTime) {
        // 获取电量低效支路
        List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordDos = getLowEfficiencyString(tenantNo, organNo, dataTime);
        analysisIUNew(tenantNo, organNo, dataTime, dateTime, stringLeDiagnoseRecordDos);
        if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordDos)) {
            Iterator<ForwardStringLeDiagnoseRecordDO> iterator = stringLeDiagnoseRecordDos.iterator();
            while (iterator.hasNext()) {
                ForwardStringLeDiagnoseRecordDO leDiagnoseRecordDo = iterator.next();
                leDiagnoseRecordDo.setCreateTime(dateTime);
                if (null == leDiagnoseRecordDo.getLowU() && null == leDiagnoseRecordDo.getLowI() &&
                        null == leDiagnoseRecordDo.getMedianI() && null == leDiagnoseRecordDo.getMedianU() &&
                        null == leDiagnoseRecordDo.getSubmatrixAvgMedianI() && null == leDiagnoseRecordDo.getSubmatrixAvgMedianU() &&
                        null == leDiagnoseRecordDo.getLossI() && null == leDiagnoseRecordDo.getLossU() && null == leDiagnoseRecordDo.getLossP()) {
                    iterator.remove();
                }
            }

            if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordDos)) {
                log.info("组串IU低效诊断分析-保存组串低效分析数据[{}, {}]", organNo, stringLeDiagnoseRecordDos.size());
                forwardStringLeDiagnoseRecordService.insertIgnoreBatch(stringLeDiagnoseRecordDos);
                log.info("组串IU低效诊断分析-保存组串低效分析数据完成[{}, {}]", organNo, stringLeDiagnoseRecordDos.size());
                CompletableFuture.runAsync(() -> {
                    log.info("组串IU低效诊断分析-异步分子子阵能效[{}, {}]", organNo, stringLeDiagnoseRecordDos.size());
                    analysisSubmatrixNew(tenantNo, organNo, stringLeDiagnoseRecordDos.get(0).getDiagnoseTime(), dateTime);
                });

            } else {
                log.info("组串IU低效诊断分析-无组串低效分析数据保存[{}]", organNo);
            }

        }
    }

    /**
     * 获取电量低效组串(支路)
     *
     * @param tenantNo
     * @param organNo
     * @param dataTime
     * @return
     */
    public List<ForwardStringLeDiagnoseRecordDO> getLowEfficiencyString(Integer tenantNo, Integer organNo, LocalDateTime dataTime) {
        // 今天 昨天 前天
        LocalDateTime endDate = LocalDateTimeUtils.getDayEnd(dataTime);
        LocalDateTime startDate = LocalDateTimeUtils.getDayStart(dataTime.minusDays(2));
        // 取近3天数据，连着3天计算结果均大于2%，则判定该支路低效(今天的结果是按照昨天往前推3天数据连续计算的结果)
        List<StringEfficiencyRecordDO> efficiencyRecordDos = stringEfficiencyRecordService.list(Wrappers.lambdaQuery(StringEfficiencyRecordDO.class)
                .eq(StringEfficiencyRecordDO::getTenantNo, tenantNo)
                .eq(StringEfficiencyRecordDO::getOrganNo, organNo)
                .ge(StringEfficiencyRecordDO::getDataTime, startDate)
                .lt(StringEfficiencyRecordDO::getDataTime, endDate)
                .gt(StringEfficiencyRecordDO::getLossE, ResParams.analysisStrLeDiagnoseLowEThreshold)
        );
        log.info("组串IU低效诊断分析-查询近3天电量损失结果大于2%的数据[{}-{}, {}, {}]", organNo, efficiencyRecordDos.size(), startDate, endDate);
        if (CollectionUtils.isNotEmpty(efficiencyRecordDos)) {
            // 按编码分组
            Map<String, List<StringEfficiencyRecordDO>> efficiencyRecordDoMap = efficiencyRecordDos.stream().collect(Collectors.groupingBy(StringEfficiencyRecordDO::getDeviceCode));
            List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordDos = new ArrayList<>(efficiencyRecordDoMap.size());
            log.info("组串IU低效诊断分析-判定低效支路开始[{}]", organNo);
            for (Map.Entry<String, List<StringEfficiencyRecordDO>> entry : efficiencyRecordDoMap.entrySet()) {
                // 连着3天计算结果均大于2%，则判定该支路低效, 则进行组串低效判定
                List<StringEfficiencyRecordDO> stringEfficiencyRecordDos = entry.getValue();
                if (stringEfficiencyRecordDos.size() >= 3) {
                    Long devicePid = entry.getValue().get(0).getDevicePid();
                    Long deviceId = entry.getValue().get(0).getDeviceId();
                    String deviceName = entry.getValue().get(0).getDeviceName();
                    String deviceCode = entry.getValue().get(0).getDeviceCode();
                    String submatrix = entry.getValue().get(0).getSubmatrix();
                    ForwardStringLeDiagnoseRecordDO leDiagnoseRecordDo = new ForwardStringLeDiagnoseRecordDO(tenantNo, organNo, devicePid, deviceId, deviceName, deviceCode, submatrix);
                    // 按照时间排序, 取最近一天的电量损失比例
                    stringEfficiencyRecordDos.sort(new Comparator<StringEfficiencyRecordDO>() {
                        @Override
                        public int compare(StringEfficiencyRecordDO o1, StringEfficiencyRecordDO o2) {
                            return o1.getDataTime().compareTo(o2.getDataTime());
                        }
                    });
                    leDiagnoseRecordDo.setLossE(stringEfficiencyRecordDos.get(stringEfficiencyRecordDos.size() - 1).getLossE());
                    leDiagnoseRecordDo.setEfficiency(stringEfficiencyRecordDos.get(stringEfficiencyRecordDos.size() - 1).getEfficiency());
                    if (StringUtils.isBlank(leDiagnoseRecordDo.getSubmatrix()) && StringUtils.isNotBlank(leDiagnoseRecordDo.getDeviceCode())) {
                        leDiagnoseRecordDo.setSubmatrix(RegexUtils.extractSubmatrixCode(leDiagnoseRecordDo.getDeviceCode()));
                    }

                    // 诊断时间
                    leDiagnoseRecordDo.setDiagnoseTime(dataTime.withHour(0).withMinute(0).withSecond(0).withNano(0));
                    stringLeDiagnoseRecordDos.add(leDiagnoseRecordDo);
                }
            }
            log.info("组串IU低效诊断分析-判定低效支路结束[{}, {}]", organNo, stringLeDiagnoseRecordDos.size());
            return stringLeDiagnoseRecordDos;
        }
        return Collections.EMPTY_LIST;
    }

    public void analysisIUNew(Integer tenantNo, Integer organNo, LocalDateTime dataTime, LocalDateTime dateTime, List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordDos) {
        if (null != tenantNo && null != organNo) {
            if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordDos)) {
                List<ForwardDeviceInfoDO> forwardDeviceInfoDos = forwardDeviceInfoService.list(Wrappers.lambdaQuery(ForwardDeviceInfoDO.class)
                        .eq(ForwardDeviceInfoDO::getTenantNo, tenantNo)
                        .eq(ForwardDeviceInfoDO::getOrganNo, organNo)
                        .eq(ForwardDeviceInfoDO::getDeviceSource, DeviceSourceEnums.SCADA.getCode())
                        .in(ForwardDeviceInfoDO::getDeviceType, Arrays.asList(DeviceTypeEnums.INVERTER.getType(), DeviceTypeEnums.COMBINER_BOX.getType(), DeviceTypeEnums.STRR.getType()))
                );
                log.info("组串IU低效诊断分析-查询所有设备信息: {}, {}", organNo, forwardDeviceInfoDos.size());
                if (CollectionUtils.isNotEmpty(forwardDeviceInfoDos)) {
                    // 按照子阵分组
                    Set<String> submatrixSet = stringLeDiagnoseRecordDos.stream().map(ForwardStringLeDiagnoseRecordDO::getSubmatrix).collect(Collectors.toSet());
                    List<String> submatrixList = new ArrayList<>(submatrixSet);
                    submatrixSet = null;
                    submatrixList.sort(Comparator.naturalOrder());
                    for (String submatrixCode : submatrixList) {
                        List<ForwardStringLeDiagnoseRecordDO> submatrixStringLeDiagnoseRecordDos = stringLeDiagnoseRecordDos.stream().filter(sldrd -> Objects.equals(submatrixCode, sldrd.getSubmatrix())).collect(Collectors.toList());
                        List<ForwardDeviceInfoDO> inverterForwardDeviceInfoDos = forwardDeviceInfoDos.stream().filter(fdid -> DeviceTypeEnums.INVERTER.getType().equals(fdid.getDeviceType()) && fdid.getDeviceCode().startsWith(submatrixCode)).collect(Collectors.toList());
                        log.info("组串IU低效诊断分析-查询到逆变器设备数量: {}, {}", organNo, inverterForwardDeviceInfoDos.size());
                        List<ForwardDeviceInfoDO> combinerboxForwardDeviceInfoDos = forwardDeviceInfoDos.stream().filter(fdid -> DeviceTypeEnums.COMBINER_BOX.getType().equals(fdid.getDeviceType()) && fdid.getDeviceCode().startsWith(submatrixCode)).collect(Collectors.toList());
                        log.info("组串IU低效诊断分析-查询到汇流箱设备数量: {}, {}", organNo, combinerboxForwardDeviceInfoDos.size());
                        List<ForwardDeviceInfoDO> stringForwardDeviceInfoDos = forwardDeviceInfoDos.stream().filter(fdid -> DeviceTypeEnums.STRR.getType().equals(fdid.getDeviceType()) && fdid.getDeviceCode().startsWith(submatrixCode)).collect(Collectors.toList());
                        log.info("组串IU低效诊断分析-查询到组串设备数量: {}, {}", organNo, stringForwardDeviceInfoDos.size());
                        if (CollectionUtils.isNotEmpty(inverterForwardDeviceInfoDos) && !ResParams.collectingDistributingSubmatrixCodes().contains(submatrixCode)) {
                            // 逆变器按照子阵分组
                            processSubmatrixForwardDeviceMap(submatrixStringLeDiagnoseRecordDos, inverterForwardDeviceInfoDos, submatrixCode, DeviceTypeEnums.INVERTER, dataTime, dateTime);
                        }
                        if (CollectionUtils.isNotEmpty(combinerboxForwardDeviceInfoDos) && !ResParams.collectingDistributingSubmatrixCodes().contains(submatrixCode)) {
                            // 汇流箱按照子阵分组
                            processSubmatrixForwardDeviceMap(submatrixStringLeDiagnoseRecordDos, combinerboxForwardDeviceInfoDos, submatrixCode, DeviceTypeEnums.COMBINER_BOX, dataTime, dateTime);
                        }
                        if (CollectionUtils.isNotEmpty(stringForwardDeviceInfoDos) && !ResParams.collectingDistributingSubmatrixCodes().contains(submatrixCode)) {
                            // 组串按照子阵分组
                            processSubmatrixForwardDeviceMap(submatrixStringLeDiagnoseRecordDos, stringForwardDeviceInfoDos, submatrixCode, DeviceTypeEnums.STRR, dataTime, dateTime);

                        }
                    }
                } else {
                    log.info("组串IU低效诊断分析-未查询到设备信息: {}, {}", organNo, forwardDeviceInfoDos.size());
                }
            } else {
                log.info("组串IU低效诊断分析-无电量低效组串数据: {}", organNo);
            }
        }
    }


    public void processSubmatrixForwardDeviceMap(List<ForwardStringLeDiagnoseRecordDO> submatrixStringLeDiagnoseRecordDos,
                                                 List<ForwardDeviceInfoDO> submatrixForwardDeviceDos,
                                                 String submatrixCode,
                                                 DeviceTypeEnums deviceTypeEnum,
                                                 LocalDateTime dataTime,
                                                 LocalDateTime dateTime) {
        if (null != submatrixStringLeDiagnoseRecordDos && submatrixStringLeDiagnoseRecordDos.size() > 0 && null != submatrixForwardDeviceDos && submatrixForwardDeviceDos.size() > 0) {
            if (null == deviceTypeEnum) {
                deviceTypeEnum = DeviceTypeEnums.INVERTER;
            }
            Integer organNo = submatrixForwardDeviceDos.get(0).getOrganNo();
            // 取当天11:00~12:00的数据
            LocalDateTime dateTimeGe = dataTime.withHour(11).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime dateTimeLt = dataTime.withHour(12).withMinute(1).withSecond(0).withNano(0);
            List<String> deviceUidIn = submatrixForwardDeviceDos.stream().map(ForwardDeviceInfoDO::getDeviceUid).collect(Collectors.toList());
            List<Long> devicePidIn = submatrixForwardDeviceDos.stream().map(ForwardDeviceInfoDO::getDevicePid).collect(Collectors.toList());
            if (DeviceTypeEnums.INVERTER == deviceTypeEnum) {
                List<Map<String, Object>> submatrixInverterDataList = null; // customSqlMapper.queryInverterDataByTmstampRange(deviceUidIn, dateTimeGe, dateTimeLt);
                log.info("组串IU低效诊断分析-查询逆变器设备电压电流数据: {}, {}, {}", submatrixForwardDeviceDos, submatrixCode, null != submatrixInverterDataList ? submatrixInverterDataList.size() : 0);
                if (CollectionUtils.isNotEmpty(submatrixInverterDataList)) {
                    List<ForwardStringDataDO> submatrixForwardStringDataDos = ForwardDeviceDataHelper.processInverterData(submatrixForwardDeviceDos, submatrixInverterDataList, dateTime);
                    log.info("组串IU低效诊断分析-转换逆变器设备电压电流数据: {}, {}, {}", organNo, submatrixCode, submatrixForwardStringDataDos.size());
                    if (CollectionUtils.isNotEmpty(submatrixForwardStringDataDos)) {
                        // 组串低效诊断
                        List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordResults = new ArrayList<>();
                        ForwardDeviceDataHelper.calculateStringIURecordBySubmatrix(organNo, submatrixCode, dataTime, dateTime,
                                submatrixForwardStringDataDos,
                                stringLeDiagnoseRecordResults);
                        if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordResults)) {
                            for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordDo : submatrixStringLeDiagnoseRecordDos) {
                                for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordResult : stringLeDiagnoseRecordResults) {
                                    if (Objects.equals(leDiagnoseRecordDo.getDeviceCode(), leDiagnoseRecordResult.getDeviceCode())) {
                                        leDiagnoseRecordDo.setMedianI(leDiagnoseRecordResult.getMedianI());
                                        leDiagnoseRecordDo.setMedianU(leDiagnoseRecordResult.getMedianU());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianI(leDiagnoseRecordResult.getSubmatrixAvgMedianI());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianU(leDiagnoseRecordResult.getSubmatrixAvgMedianU());
                                        leDiagnoseRecordDo.setLossI(leDiagnoseRecordResult.getLossI());
                                        leDiagnoseRecordDo.setLossU(leDiagnoseRecordResult.getLossU());
                                        leDiagnoseRecordDo.setLossP(leDiagnoseRecordResult.getLossP());
                                        leDiagnoseRecordDo.setLowI(leDiagnoseRecordResult.getLowI());
                                        leDiagnoseRecordDo.setLowU(leDiagnoseRecordResult.getLowU());
                                        leDiagnoseRecordDo.setSubmatrixStringNum(leDiagnoseRecordResult.getSubmatrixStringNum());
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        log.info("组串IU低效诊断分析-转换逆变器设备电压电流数据后无数据: {}, {}", organNo, submatrixCode);
                    }
                } else {
                    log.info("组串IU低效诊断分析-未查询到逆变器设备电压电流数据: {}, {}", submatrixForwardDeviceDos, submatrixCode);
                }
            } else if (DeviceTypeEnums.COMBINER_BOX == deviceTypeEnum) {
                List<Map<String, Object>> submatrixCombinerboxDataList = null; // customSqlMapper.queryCombinerboxDataByTmstampRange(deviceUidIn, dateTimeGe, dateTimeLt);
                log.info("组串IU低效诊断分析-查询汇流箱设备电压电流数据: {}, {}, {}", organNo, submatrixCode, null != submatrixCombinerboxDataList ? submatrixCombinerboxDataList.size() : 0);
                if (CollectionUtils.isNotEmpty(submatrixCombinerboxDataList)) {
                    List<ForwardStringDataDO> submatrixForwardStringDataDos = ForwardDeviceDataHelper.processCombinerboxData(submatrixForwardDeviceDos, submatrixCombinerboxDataList, dateTime);
                    log.info("组串IU低效诊断分析-转换汇流箱设备电压电流数据: {}, {}, {}", organNo, submatrixCode, submatrixForwardStringDataDos.size());
                    if (CollectionUtils.isNotEmpty(submatrixForwardStringDataDos)) {
                        // 组串低效诊断
                        List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordResults = new ArrayList<>();
                        ForwardDeviceDataHelper.calculateStringIURecordBySubmatrix(organNo, submatrixCode, dataTime, dateTime,
                                submatrixForwardStringDataDos,
                                stringLeDiagnoseRecordResults);
                        if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordResults)) {
                            for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordDo : submatrixStringLeDiagnoseRecordDos) {
                                for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordResult : stringLeDiagnoseRecordResults) {
                                    if (Objects.equals(leDiagnoseRecordDo.getDeviceCode(), leDiagnoseRecordResult.getDeviceCode())) {
                                        leDiagnoseRecordDo.setMedianI(leDiagnoseRecordResult.getMedianI());
                                        leDiagnoseRecordDo.setMedianU(leDiagnoseRecordResult.getMedianU());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianI(leDiagnoseRecordResult.getSubmatrixAvgMedianI());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianU(leDiagnoseRecordResult.getSubmatrixAvgMedianU());
                                        leDiagnoseRecordDo.setLossI(leDiagnoseRecordResult.getLossI());
                                        leDiagnoseRecordDo.setLossU(leDiagnoseRecordResult.getLossU());
                                        leDiagnoseRecordDo.setLossP(leDiagnoseRecordResult.getLossP());
                                        leDiagnoseRecordDo.setLowI(leDiagnoseRecordResult.getLowI());
                                        leDiagnoseRecordDo.setLowU(leDiagnoseRecordResult.getLowU());
                                        leDiagnoseRecordDo.setSubmatrixStringNum(leDiagnoseRecordResult.getSubmatrixStringNum());
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        log.info("组串IU低效诊断分析-转换汇流箱设备电压电流数据后无数据: {}, {}", organNo, submatrixCode);
                    }
                } else {
                    log.info("组串IU低效诊断分析-未查询到汇流箱设备电压电流数据: {}, {}", organNo, submatrixCode);
                }
            } else if (DeviceTypeEnums.STRR == deviceTypeEnum) {
                List<Map<String, Object>> submatrixCombinerboxDataList = null; // customSqlMapper.queryCombinerboxDataByTmstampRange(deviceUidIn, dateTimeGe, dateTimeLt);
                List<ForwardStringDataCurrentdayDO> currentdayDos = forwardStringDataCurrentdayService.list(Wrappers.lambdaQuery(ForwardStringDataCurrentdayDO.class)
                        .in(ForwardStringDataCurrentdayDO::getDevicePid, devicePidIn).ge(ForwardStringDataCurrentdayDO::getDataTime, dateTimeGe).lt(ForwardStringDataCurrentdayDO::getDataTime, dateTimeLt));
                log.info("组串IU低效诊断分析-查询组串设备电压电流数据: {}, {}, {}", organNo, submatrixCode, currentdayDos.size());
                if (CollectionUtils.isNotEmpty(currentdayDos)) {
                    List<ForwardStringDataDO> submatrixForwardStringDataDos = ObjectMapper.mapList(currentdayDos, ForwardStringDataDO.class);
                    log.info("组串IU低效诊断分析-转换汇流箱设备电压电流数据: {}, {}, {}", organNo, submatrixCode, null != submatrixForwardStringDataDos ? submatrixForwardStringDataDos.size() : 0);
                    if (CollectionUtils.isNotEmpty(submatrixForwardStringDataDos)) {
                        // 组串低效诊断
                        List<ForwardStringLeDiagnoseRecordDO> stringLeDiagnoseRecordResults = new ArrayList<>();
                        ForwardDeviceDataHelper.calculateStringIURecordBySubmatrix(organNo, submatrixCode, dataTime, dateTime,
                                submatrixForwardStringDataDos,
                                stringLeDiagnoseRecordResults);
                        if (CollectionUtils.isNotEmpty(stringLeDiagnoseRecordResults)) {
                            for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordDo : submatrixStringLeDiagnoseRecordDos) {
                                for (ForwardStringLeDiagnoseRecordDO leDiagnoseRecordResult : stringLeDiagnoseRecordResults) {
                                    if (Objects.equals(leDiagnoseRecordDo.getDeviceCode(), leDiagnoseRecordResult.getDeviceCode())) {
                                        leDiagnoseRecordDo.setMedianI(leDiagnoseRecordResult.getMedianI());
                                        leDiagnoseRecordDo.setMedianU(leDiagnoseRecordResult.getMedianU());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianI(leDiagnoseRecordResult.getSubmatrixAvgMedianI());
                                        leDiagnoseRecordDo.setSubmatrixAvgMedianU(leDiagnoseRecordResult.getSubmatrixAvgMedianU());
                                        leDiagnoseRecordDo.setLossI(leDiagnoseRecordResult.getLossI());
                                        leDiagnoseRecordDo.setLossU(leDiagnoseRecordResult.getLossU());
                                        leDiagnoseRecordDo.setLossP(leDiagnoseRecordResult.getLossP());
                                        leDiagnoseRecordDo.setLowI(leDiagnoseRecordResult.getLowI());
                                        leDiagnoseRecordDo.setLowU(leDiagnoseRecordResult.getLowU());
                                        leDiagnoseRecordDo.setSubmatrixStringNum(leDiagnoseRecordResult.getSubmatrixStringNum());
                                        break;
                                    }
                                }
                            }
                        }
                    } else {
                        log.info("组串IU低效诊断分析-转换汇流箱设备电压电流数据后无数据: {}, {}", organNo, submatrixCode);
                    }
                } else {
                    log.info("组串IU低效诊断分析-未查询到汇流箱设备电压电流数据: {}, {}", organNo, submatrixCode);
                }
            }
        }
    }


    @org.springframework.scheduling.annotation.Async
    public void analysisSubmatrixNew(Integer tenantNo, Integer organNo, LocalDateTime diagnoseTime, LocalDateTime now) {
        log.info("组串IU低效诊断分析-子阵分析[{}, {}]", organNo);
        QueryWrapper<ForwardStringLeDiagnoseRecordDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ForwardStringLeDiagnoseRecordDO::getTenantNo, tenantNo)
                .eq(ForwardStringLeDiagnoseRecordDO::getOrganNo, organNo)
                .eq(ForwardStringLeDiagnoseRecordDO::getDiagnoseTime, diagnoseTime);
        List<ForwardStringLeDiagnoseRecordDO> leDiagnoseRecordDos = forwardStringLeDiagnoseRecordService.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(leDiagnoseRecordDos)) {
            Set<String> submatrixSet = leDiagnoseRecordDos.stream().map(ForwardStringLeDiagnoseRecordDO::getSubmatrix).collect(Collectors.toSet());
            List<ForwardSubmatrixLeRecordDO> results = new ArrayList<>(submatrixSet.size());
            for (String submatrix : submatrixSet) {
                ForwardSubmatrixLeRecordDO submatrixLeRecordDo = new ForwardSubmatrixLeRecordDO(tenantNo, organNo, submatrix, submatrix,
                        submatrix, diagnoseTime, now);
                List<ForwardStringLeDiagnoseRecordDO> subLeDiagnoseRecordDos = leDiagnoseRecordDos.stream().filter(rd -> Objects.equals(submatrix, rd.getSubmatrix())).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(subLeDiagnoseRecordDos)) {
                    submatrixLeRecordDo.setAvgI(subLeDiagnoseRecordDos.get(0).getSubmatrixAvgI());
                    submatrixLeRecordDo.setAvgU(subLeDiagnoseRecordDos.get(0).getSubmatrixAvgU());
                    submatrixLeRecordDo.setAvgMedianI(subLeDiagnoseRecordDos.get(0).getSubmatrixAvgMedianI());
                    submatrixLeRecordDo.setAvgMedianU(subLeDiagnoseRecordDos.get(0).getSubmatrixAvgMedianU());
                    submatrixLeRecordDo.setSubmatrixStringNum(subLeDiagnoseRecordDos.get(0).getSubmatrixStringNum());
                    Double lossI = subLeDiagnoseRecordDos.stream().filter(drd -> null != drd.getLossI()).mapToDouble(ForwardStringLeDiagnoseRecordDO::getLossI).average().orElse(0);
                    submatrixLeRecordDo.setLossI(lossI);
                    Double lossU = subLeDiagnoseRecordDos.stream().filter(drd -> null != drd.getLossU()).mapToDouble(ForwardStringLeDiagnoseRecordDO::getLossU).average().orElse(0);
                    submatrixLeRecordDo.setLossU(lossU);
                    Double lossP = subLeDiagnoseRecordDos.stream().filter(drd -> null != drd.getLossP()).mapToDouble(ForwardStringLeDiagnoseRecordDO::getLossP).average().orElse(0);
                    submatrixLeRecordDo.setLossP(lossP);
                    Double lossE = subLeDiagnoseRecordDos.stream().filter(drd -> null != drd.getLossE()).mapToDouble(ForwardStringLeDiagnoseRecordDO::getLossE).average().orElse(0);
                    submatrixLeRecordDo.setLossE(lossE);
                    // 电量损失
                    // 低效电流
                    List<ForwardStringLeDiagnoseRecordDO> subDiagnoseRecordDos = subLeDiagnoseRecordDos.stream().filter(drd -> Objects.equals(submatrix, drd.getSubmatrix()) &&
                            (null != drd.getLowI() && drd.getLowI() > ResParams.analysisStrLeDiagnoseLowIThreshold) &&
                            (null == drd.getLowU() || drd.getLowU() <= ResParams.analysisStrLeDiagnoseLowUThreshold)
                    ).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(subDiagnoseRecordDos)) {
                        submatrixLeRecordDo.setLowINum(subDiagnoseRecordDos.size());
                    }
                    // 低效电压
                    subDiagnoseRecordDos = subLeDiagnoseRecordDos.stream().filter(drd -> Objects.equals(submatrix, drd.getSubmatrix()) &&
                            (null != drd.getLowU() && drd.getLowU() > ResParams.analysisStrLeDiagnoseLowUThreshold) &&
                            (null == drd.getLowI() || drd.getLowI() <= ResParams.analysisStrLeDiagnoseLowUThreshold)
                    ).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(subDiagnoseRecordDos)) {
                        submatrixLeRecordDo.setLowUNum(subDiagnoseRecordDos.size());
                    }
                    // 低效电流电压
                    subDiagnoseRecordDos = subLeDiagnoseRecordDos.stream().filter(drd -> Objects.equals(submatrix, drd.getSubmatrix()) &&
                            (null != drd.getLowU() && drd.getLowU() > ResParams.analysisStrLeDiagnoseLowUThreshold) &&
                            (null != drd.getLowI() && drd.getLowI() > ResParams.analysisStrLeDiagnoseLowIThreshold)
                    ).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(subDiagnoseRecordDos)) {
                        submatrixLeRecordDo.setLowIUNum(subDiagnoseRecordDos.size());
                    }
                }
                results.add(submatrixLeRecordDo);
            }
            if (CollectionUtils.isNotEmpty(results)) {
                results.sort(Comparator.comparing(ForwardSubmatrixLeRecordDO::getSubmatrix));
                forwardSubmatrixLeRecordService.insertIgnoreBatch(results);
            }
        }
    }

    /**
     * 组串能效分析
     *
     * @param tenantNo
     * @param organNo
     * @param dataTime
     * @param interval 默认2, 即连续3天
     */
    public java.util.Set<String> analysisStringEfficiency(Integer tenantNo, Integer organNo, LocalDateTime dataTime, Long interval, String taskNo, String taskSubNo, LocalDateTime now) {
        if (null != organNo && null != dataTime) {
            if (null == interval) {
                interval = 2L;
            }
            // int[] = {2024, 10, 6}
            final List<Integer[]> dayArrs = LocalDateTimeUtils.getYearMonthDayIntArr(dataTime.minusDays(interval), dataTime);
            List<StringEfficiencyDayDO> stringEfficiencyDayDos = stringEfficiencyDayService.list(Wrappers.lambdaQuery(StringEfficiencyDayDO.class)
                    .eq(null != tenantNo, StringEfficiencyDayDO::getTenantNo, tenantNo)
                    .eq(StringEfficiencyDayDO::getOrganNo, organNo)
                    .and(wr -> {
                        for (int i = 0; i < dayArrs.size(); i++) {
                            Integer[] dayArr = dayArrs.get(i);
                            wr.or(wr1 -> wr1.eq(StringEfficiencyDayDO::getYear, dayArr[0].shortValue())
                                    .eq(StringEfficiencyDayDO::getMonth, dayArr[1].byteValue())
                                    .eq(StringEfficiencyDayDO::getDay, dayArr[2].byteValue()));
                        }
                    }).orderByAsc(StringEfficiencyDayDO::getYear, StringEfficiencyDayDO::getMonth, StringEfficiencyDayDO::getDay)
            );
            if (null != stringEfficiencyDayDos && stringEfficiencyDayDos.size() > 0) {
                // 按组串分组 devicePid-deviceId, 也可以根据deviceCode分组
//                Map<String, List<StringEfficiencyDayDO>> stringEfficiencyMap = stringEfficiencyDayDos.stream().collect(Collectors.groupingBy(d -> (d.getDevicePid().toString()).concat("-").concat(d.getDeviceId().toString())));
//                for (Map.Entry<String, List<StringEfficiencyDayDO>> entry : stringEfficiencyMap.entrySet()) {
//                    if (null != entry.getValue() && entry.getValue().size() >= 3) {
//
//                    }
//                }
                // 组串低效计数
                Map<String, Integer> lowEfficiencyCountMap = new HashMap<>(stringEfficiencyDayDos.size());
                for (Integer[] dayArr : dayArrs) {
                    List<StringEfficiencyDayDO> subStringEfficiencyDayDos = stringEfficiencyDayDos.stream().filter(sed -> Objects.equals(sed.getYear(), dayArr[0]) && Objects.equals(sed.getMonth(), dayArr[1]) && Objects.equals(sed.getDay(), dayArr[2])).collect(Collectors.toList());
                    if (null != subStringEfficiencyDayDos && subStringEfficiencyDayDos.size() > 0) {
                        // 按子阵分组, 计算出每个子阵的平均能效
                        // 判断每个组串能效是否小于该子阵的平均能效的比例
                        Map<String, List<StringEfficiencyDayDO>> submatrixStringEfficiencyMap = stringEfficiencyDayDos.stream().collect(Collectors.groupingBy(d -> (d.getDeviceCode().substring(0, 4))));
                        for (Map.Entry<String, List<StringEfficiencyDayDO>> submatrixEntry : submatrixStringEfficiencyMap.entrySet()) {
                            OptionalDouble submatrixAvgEfficiencyOptional = submatrixEntry.getValue().stream().filter(d -> null != d.getEfficiency()).map(StringEfficiencyDayDO::getEfficiency).mapToDouble(Double::valueOf).average();
                            if (submatrixAvgEfficiencyOptional.isPresent()) {
                                for (StringEfficiencyDayDO stringEfficiencyDayDo : submatrixEntry.getValue()) {
                                    if (null != stringEfficiencyDayDo.getEfficiency()) {
                                        // 计算单个组串能效与子阵平均能效的比值
                                        double rate = (1D - stringEfficiencyDayDo.getEfficiency() / submatrixAvgEfficiencyOptional.getAsDouble()) * 100D;
                                        // 如果比值大于阈值 默认为10%
                                        if (rate >= ResParams.analysisStrLeDiagnoseLowEThreshold) {
                                            Integer lowCnt = lowEfficiencyCountMap.get(stringEfficiencyDayDo.getDeviceCode());
                                            if (lowCnt == null) {
                                                lowCnt = 0;
                                            }
                                            lowEfficiencyCountMap.put(stringEfficiencyDayDo.getDeviceCode(), lowCnt + 1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                java.util.Set<String> resultSet = new HashSet<>();
                List<ForwardStringLeDiagnoseDetailDO> diagnoseDetailDos = new ArrayList<>();
                for (Map.Entry<String, Integer> countEntry : lowEfficiencyCountMap.entrySet()) {
                    if (null != countEntry.getValue() && countEntry.getValue() >= 3) {
                        resultSet.add(countEntry.getKey());
                        if (StringUtils.isNotBlank(taskNo) && StringUtils.isNotBlank(taskSubNo)) {
                            StringEfficiencyDayDO dayDo = stringEfficiencyDayDos.stream().filter(sed -> Objects.equals(sed.getDeviceCode(), countEntry.getKey())).findFirst().orElse(null);
                            ForwardStringLeDiagnoseDetailDO diagnoseDetailDo = new ForwardStringLeDiagnoseDetailDO();
                            diagnoseDetailDo.setTenantNo(dayDo.getTenantNo());
                            diagnoseDetailDo.setOrganNo(organNo);
                            diagnoseDetailDo.setDeviceCode(countEntry.getKey());
                            diagnoseDetailDo.setDevicePid(dayDo.getDevicePid());
                            diagnoseDetailDo.setDeviceId(diagnoseDetailDo.getDeviceId());
                            diagnoseDetailDo.setCreateTime(now);
                            diagnoseDetailDo.setResult(StateEnums.ENABLE.getEnable());
                            diagnoseDetailDo.setState(StateEnums.ENABLE.getEnable());
                            diagnoseDetailDo.setTaskNo(taskNo);
                            diagnoseDetailDo.setTaskSubNo(taskSubNo);
                            diagnoseDetailDos.add(diagnoseDetailDo);
                        }
                    }
                }
                if (null != diagnoseDetailDos && diagnoseDetailDos.size() > 0) {
                    log.info("保存低效组串结果: {}", diagnoseDetailDos.size());
                    forwardStringLeDiagnoseDetailService.saveBatch(diagnoseDetailDos);
                }
                return resultSet;
            }
        }
        return Collections.EMPTY_SET;
    }
}
